package model;

/**
 * ToClosestShipping is a card that takes the player to the nearest shipping and makes him pay double the rent if the field is owned.
 * @author Group 20
 *
 */

public class ToClosestShipping extends Move
{
	private int payment, shipping1FieldNum = 6, shipping2FieldNum = 16, shipping3FieldNum = 26, shipping4FieldNum = 36;
	Field[] fields;
	
	/**
	 * Constructor
	 * @param description: Card description
	 * @param fields: array of fields
	 */
	public ToClosestShipping(String description, Field[] fields) {
		super(description);
		this.fields = fields;
	}
	
	/**
	 * Executes the cards action
	 */
	@Override
	public boolean draw(Player player) {
		// Move
		int currentPosition = player.getStandOnField();
		if(currentPosition <= 6 || currentPosition > 36)
		{
			player.setStandOnField(shipping1FieldNum);
		}
		else if(currentPosition <= 16)
		{
			player.setStandOnField(shipping2FieldNum);
		}
		else if(currentPosition <= 26)
		{
			player.setStandOnField(shipping3FieldNum);
		}
		else
		{
			player.setStandOnField(shipping4FieldNum);
		}
		
		Field field = fields[player.getStandOnField()-1];
		Ownable shippingField = ((Ownable)field);
		if(shippingField.isOwned())
		{
			System.out.println("owned");
			int rent = shippingField.rent() * 2;
			System.out.println("rent: " + rent);
			
			if(player != shippingField.getOwner())
			{
				player.setCarryOn(false);
				
				payment = -rent;
				if(player.getBalance() >= payment)
				{
					player.withdraw(rent);
					shippingField.getOwner().deposit(rent);
				}
				else
					return false;
			}
			else
				payment = 0;
		}
		else
		{
			payment = 0;
		}
		return true;
	}

	/**
	 * toString Method
	 */
	@Override
	public String toString() {
		return "ToClosestShipping [shipping1FieldNum=" + shipping1FieldNum
				+ ", shipping2FieldNum=" + shipping2FieldNum
				+ ", shipping3FieldNum=" + shipping3FieldNum
				+ ", shipping4FieldNum=" + shipping4FieldNum + super.toString() +"]";
	}	

}
